package org.example.config;

import org.example.discovery.Discovery;
import org.example.discovery.IServiceDiscovery;
import org.example.runner.DiscoveryApplicationRunner;
import org.example.runner.UnDiscoveryRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.http.client.*;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;


/**
 * @author sxt
 * @version 1.0
 * @description 服务注册与发现组件 配置
 * @date 2023/8/3 23:03
 */
@Import({
        DiscoveryProperties.class,
        DiscoveryApplicationRunner.class,
        UnDiscoveryRunner.class
})
public class DiscoveryConfiguration {

    @Bean
    @ConditionalOnMissingBean(RestTemplate.class)
    public RestTemplate addRestTemplate(ClientHttpRequestFactory httpRequestFactory) {
        return new RestTemplate(httpRequestFactory);
    }

    @Bean
    @ConditionalOnMissingBean(ClientHttpRequestFactory.class)
    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(150000); // ms
        factory.setConnectTimeout(150000); // ms
        return factory;
    }

    @Bean
    @ConditionalOnMissingBean(IServiceDiscovery.class)
    public IServiceDiscovery addServiceDiscovery() {
        return new Discovery();
    }

}
